package com.fastloan.mgt.application.approve.service.impl;

import com.fastloan.mgt.application.base.model.Application;
import com.fastloan.mgt.application.base.service.ApplicationService;
import com.fastloan.mgt.customer.model.CustomerAmount;
import com.fastloan.mgt.customer.service.CustomerAmountService;
import com.fastloan.mgt.application.approve.service.CreditApproveService;
import com.fastloan.mgt.processcontrol.base.annotation.FunctionHandlerClass;
import com.fastloan.mgt.processcontrol.base.annotation.FunctionHandlerMethod;
import com.fastloan.mgt.processcontrol.base.model.ProcessSession;
import com.fastloan.mgt.processcontrol.base.service.ProcessDispatcher;
import com.fastloan.mgt.product.flow.enums.ProductFunctionGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

import static com.fastloan.mgt.application.base.enums.ApplicationStatusEnum.*;

/**
 * @author chenkai
 * createTime 2018-01-2018/1/9 10:17
 */
@FunctionHandlerClass(processName = "预授信审批", bmpIds = {ProductFunctionGroup.GROUP_ONE})
@Component
public class CreditApproveServiceImpl implements CreditApproveService {
    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private CustomerAmountService customerAmountService;

    @FunctionHandlerMethod
    @Transactional(rollbackFor = Exception.class)
    public void runApprove(String processId) {
        ProcessSession processSession = ProcessDispatcher.getProcessSession(processId);
        String applicationId = processSession.getAttribute("applicationId", String.class);
        Application application = applicationService.selectByPrimaryKey(applicationId);
        application.setApplicationStatus(PRELIMINARY_CREDIT.getType());
        applicationService.updateByPrimaryKeySelective(application);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approve(CustomerAmount creditAmount, Integer status) {
        Application application = applicationService.selectByPrimaryKey(creditAmount.getApplicationId());
        if (application == null || (!application.getApplicationStatus().equals(WAIT_SURVEY.getType())
                && !application.getApplicationStatus().equals(APP_APPROVE.getType())
                && !application.getApplicationStatus().equals(PRELIMINARY_CREDIT.getType()))) {
            throw new RuntimeException("非法请求, 不是正确的审批状态");
        }
        if (status.equals(1)) {
            creditAmount.setStatus(1);
            creditAmount.setOriginalAmount(creditAmount.getCreditAmount());
            customerAmountService.updateByPrimaryKeySelective(creditAmount);
            application.setApplicationStatus(PRELIMINARY_CREDIT_PASS.getType());
            applicationService.updateByPrimaryKeySelective(application);
            ProcessDispatcher.executeEnd(application.getProcessId(), true);
        } else {
            creditAmount.setStatus(2);
            application.setApplicationStatus(PRELIMINARY_CREDIT_NO_PASS.getType());
            applicationService.updateByPrimaryKeySelective(application);
            creditAmount.setCreditAmount(BigDecimal.ZERO);
            customerAmountService.updateByPrimaryKeySelective(creditAmount);
        }
        ProcessDispatcher.executeEnd(application.getProcessId(), false);
    }
}
